<?php

/**
 * Created by IntelliJ IDEA.
 * User: keke
 * Date: 18-10-27
 * Time: 下午12:43
 */

require_once "../congfig/config.php";
require_once "../lib/common.php";
$pname = $_POST['pname'];
$pid = $_POST['pid'];
$pChar = $_POST['pChar'];
$teacher = $_POST['teacher'];
$pDesc = $_POST['pDesc'];
$pRank = $_POST['pRank'];
@$file = $_FILES['Enclosure'];
if ($pChar == null || $teacher == null || $pDesc == null) {
    exit(jsoncall("-1", "信息不能为空"));
}
if (!isset($file)) {
    $sql = "SELECT * FROM `users` WHERE username = '$pChar'";
    $query = $conn->query($sql);
    $relsut = $query->fetch_array();
    $uid = $relsut['uid'];
    $sql = "SELECT * FROM `puser` WHERE uid='$uid'and pid='$pid' ";
    $query = $conn->query($sql);
    $num = $query->num_rows;
    if ($num == 1) {
        $sql = "UPDATE proinfor SET
            pName ='$pname' ,pChar='$pChar',teacher='$teacher',pRank='$pRank' ,pDesc='$pDesc' WHERE id ='$pid'";

        $query = $conn->query($sql);

        if ($query) {
            exit(jsoncall(1, "修改项目成功"));
        } else {
            exit(jsoncall(-1, "修改项目失败"));
        }
    } else {
        exit(jsoncall(-1, "项目内无此成员"));
    }

}
$sql = "SELECT * FROM proinfor WHERE id = '$pid'";
$query = $conn->query($sql);
// $num = $query->num_rows;
// if ($num >= 1) {
//     exit(jsoncall("-1", "项目已经存在"));
// }

$result = $query->fetch_array();
if (isset($result['Enclosure'])) {
    $files = scandir(filepath);
    for ($i = 0; $i < count($files); $i++) {
        if ($files[$i] === $result['Enclosure']) {
            unlink(filepath . $files[$i]);
            break;
        }
    }
}
$file = uploadfile($file, maxSize, filepath);
$sql = "SELECT * FROM `users` WHERE username = '$pChar'";
$query = $conn->query($sql);
$relsut = $query->fetch_array();
$uid = $relsut['uid'];
$sql = "SELECT * FROM `puser` WHERE uid='$uid'and pid='$pid' ";
$query = $conn->query($sql);
$num = $query->num_rows;
if ($num === 1) {
    $sql = "UPDATE proinfor SET
pName ='$pname',pChar='$pChar',teacher='$teacher',pRank='$pRank',pDesc='$pDesc',Enclosure = '$file'  WHERE id ='$pid'";
    $query = $conn->query($sql);
    if ($query) {
        exit(jsoncall(1, "修改项目成功"));
    } else {
        exit(jsoncall(-1, "修改项目失败"));
    }
} else {
    exit(jsoncall(-1, "项目内无此成员"));
}
